﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;

public partial class cms_Admin_Denghi_u_Denghi : System.Web.UI.UserControl
{
    #region[Declare varian]
    static string ID = "";
    static string insert = "";
    string p = "";
    string uID = "";
    #endregion
    #region[Page_Load]
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["p"] != null) { p = Session["p"].ToString(); }
        if (Session["uid"] != null) { uID = Session["uid"].ToString(); }
        if (!IsPostBack) { Viewnguoinhan(); }
    }
    #endregion
    #region[Viewnguoinhan]
    void Viewnguoinhan()
    {
        drlNguoinhan.Items.Clear();
        drlNguoinhan.Items.Add(new ListItem("===Chọn người nhận===","0"));
        //List<tbUsersDATA> list = tbUserDB.tbUsers_GetByCheckLevel(p);
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetByAll();
        for (int i = 0; i < list.Count; i++)
        {
            drlNguoinhan.Items.Add(new ListItem(list[i].uName, list[i].uID));
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[Senddenghi]
    protected void lbtCapnhat_Click(object sender, EventArgs e)
    {
        if (Test() == true)
        {
            string img_path = "";
            if (fileupload.PostedFile != null && fileupload.PostedFile.ContentLength > 0)
            {
                img_path = System.IO.Path.GetFileName(fileupload.PostedFile.FileName);
                fileupload.PostedFile.SaveAs(Server.MapPath("Users/files/") + img_path.ToString().Trim());
                img_path = "Users/files/" + img_path.ToString().Trim();
            }
            tbProposalDATA ObjtbProposalDATA = new tbProposalDATA();
            ObjtbProposalDATA.uSend = uID;
            ObjtbProposalDATA.uRec = drlNguoinhan.SelectedValue;
            ObjtbProposalDATA.prName = txtTieude.Text;
            ObjtbProposalDATA.prContent = fckNoidung.Value;
            ObjtbProposalDATA.prFiles = img_path;
            try
            {
                if (tbProposalDB.tbProposal_Add(ObjtbProposalDATA))
                {
                    lblLoi.Text = "Gửi đề nghị thành công !!";
                    Resetcontrol();
                    insert = "";
                }
            }
            catch { lblLoi.Text = "Lỗi dữ liệu nhập vào !!"; return; }
        }
    }
    #endregion
    #region[Test]
    protected bool Test()
    {
        if (drlNguoinhan.SelectedValue == "0") { lblLoi.Text = "Vui lòng chọn người nhận đề nghị !!"; return false; }
        if (txtTieude.Text == "") { lblLoi.Text = "Vui lòng nhập tiêu đề đề nghị !!"; return false; }
        return true;
    }
    #endregion
    #region[Back]
    protected void lbtTrolai_Click(object sender, EventArgs e)
    {

    }
    #endregion
    #region[Resetcontrol]
    void Resetcontrol()
    {
        drlNguoinhan.SelectedValue = "0";
        txtTieude.Text = "";
        fckNoidung.Value = "";
    }
    #endregion
}
